// Created by Xslt generator for Eclipse.
// XSL :  not found (java.io.FileNotFoundException:  (Bad file descriptor))
// Default XSL used : easystruts.jar$org.easystruts.xslgen.JavaClass.xsl

package com.ba360.struts.action;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.naming.Context;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ba360.portal.common.PortalConstants;
import com.ba360.portal.database.DBConnectionImpl;
//import com.ba360.portal.database.dbo.User;
import com.ba360.portal.database.dbo.User_23Feb09;
import com.ba360.portal.util.Log;
import com.ba360.struts.form.LoginForm;
import com.ba360.struts.form.LoginForm_23Feb09;

/**
 * LoginAction.java created by EasyStruts - XsltGen. http://easystruts.sf.net
 * created on 04-02-2004
 * 
 * XDoclet definition:
 * 
 * @struts:action path="/login" name="LoginForm" input="/jsp/Login.jsp"
 *                validate="true"
 * @struts:action-forward name="/jsp/Login.jsp" path="/jsp/Login.jsp"
 * @struts:action-forward name="/jsp/Portal.jsp" path="/jsp/Portal.jsp"
 */
public class LoginAction_23Feb09 extends Action {

	// --------------------------------------------------------- Instance
	// Variables

	// --------------------------------------------------------- Methods

	/**
	 * Method execute
	 * 
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("Value of action parameter in request query string is:"+request.getQueryString().toString());
		System.out.println("Value of action parameter in request query string is:"+request.getRequestURI().toString());
		
		String action1 = (String) request.getParameter(PortalConstants.ACTION);
		//throw new UnsupportedOperationException("Generated method
		// 'execute(...)' not implemented.");
		Log.init(this.getClass());
		String action = request.getParameter(PortalConstants.ACTION);
		HttpSession session = request.getSession();
		Log.info(request.getHeader("referer"));
		if (action == null || !action.equals("Login")) {
			response.setHeader("referer",request.getHeader("referer"));
			session.setAttribute("referer",request.getHeader("referer"));
			return mapping.findForward("failure");
		}

		if (action.equals("Login")) {
			session.removeAttribute("referer");
			Context context = null;
			DataSource ds = null;
			Connection conn = null;
			Statement stmt = null;

			try {

				DBConnectionImpl dbConn = new DBConnectionImpl();
				conn = dbConn.getConnection();
				//Class.forName("org.gjt.mm.mysql.Driver");
				//Connection con
				// =DriverManager.getConnection("jdbc:mysql://localhost:3306/classifiedsdb","portal","p0rtal");
				stmt = conn.createStatement();
				//LoginForm lf = (LoginForm) form; Modified by Arun on 08-MAY-2009
				LoginForm_23Feb09 lf = (LoginForm_23Feb09) form;
				String userName = lf.getUserName();
				String password = lf.getPassword();
				double userId;
				//HttpSession session = request.getSession();
				session.removeAttribute(PortalConstants.USER);
				request.removeAttribute("PASS_NOT_MATCH");
				request.removeAttribute("USER_NOT_EXIST");
				ResultSet rs = stmt
						.executeQuery("select userid from userinfo where emailaddress='"
								+ userName.trim() + "'");
				while (rs.next()) {
					//rs.next();
					userId = rs.getDouble(1);
					rs.close();
					rs = stmt
							.executeQuery("select password from userauth where userid="
									+ userId);
					rs.next();
					String pwd = rs.getString(1);
					System.out.println("Value of pwd is:"+pwd);
					System.out.println("Value of password in form is :"+lf.getPassword().toString());
					if(pwd == lf.getPassword().toString())
							{
						boolean isEqual = true;
					System.out.println("Value of isEqual is "+isEqual);
							}
					if (pwd.equals(lf.getPassword().toString())) {
						// User user = populateUserInfo(conn, userId, pwd); Modified by Arun on 08- MAY-2009
						System.out.println("I am entered into IF loop...");
						User_23Feb09 user = populateUserInfo(conn, userId, pwd); 
						session.setAttribute(PortalConstants.USER, user);
						PublishAdvAction pub = new PublishAdvAction();
						request.setAttribute(PortalConstants.ACTION, "Display");
						ActionForward am = pub.execute(mapping, form, request,
								response);
						return am;
					} else {
						request
								.setAttribute("PASS_NOT_MATCH",
										"PASS_NOT_MATCH");
						return mapping.findForward("failure");
					}

				}
				request.setAttribute("USER_NOT_EXIST", "USER_NOT_EXIST");
				conn.close();
				return mapping.findForward("failure");

			} catch (Exception e) {
				Log.error(e.getMessage());
				return mapping.findForward("failure");
			}finally{
				conn.close();
			}

		}

		return mapping.findForward("failure");
	}

	// public User populateUserInfo(Connection con, double userId, String password) {  Modified by Arun on 08-MAY-2009
	public User_23Feb09 populateUserInfo(Connection con, double userId, String password) {
		//User user = new User();
		User_23Feb09 user = new User_23Feb09(); // Modified by Arun on 08-MAY-2009
		user.setUserId(userId);
		user.setPwd(password);
		ResultSet rs = null;
		Statement stmt = null;
		try {

			stmt = con.createStatement();
			rs = stmt.executeQuery("select * from userinfo where userid="
					+ userId);
			rs.next();
			
			//user.setBday(rs.getString(User.USER_BDAY)); Modified by Arun on 08-MAY-2009
			user.setBday(rs.getString(User_23Feb09.USER_BDAY));	
			//user.setCity(rs.getString(User.USER_CITY));		Modified by Arun on 08-MAY-2009
			user.setCity(rs.getString(User_23Feb09.USER_CITY));
			//user.setCountry(rs.getString(User.USER_COUNTRY));		   	Modified by Arun on 08-MAY-2009
			user.setCountry(rs.getString(User_23Feb09.USER_COUNTRY));  
			//user.setEmail(rs.getString(User.USER_EMAIL));				Modified by Arun on 08-MAY-2009
			user.setEmail(rs.getString(User_23Feb09.USER_EMAIL));		
			//user.setFN(rs.getString(User.USER_FN));					Modified by Arun on 08-MAY-2009
			user.setFN(rs.getString(User_23Feb09.USER_FN));			    
			//user.setGender(rs.getString(User.USER_GENDER));			Modified by Arun on 08-MAY-2009
			user.setGender(rs.getString(User_23Feb09.USER_GENDER));		
			//user.setLN(rs.getString(User.USER_LN));					Modified by Arun on 08-MAY-2009
			user.setLN(rs.getString(User_23Feb09.USER_LN));				
			//user.setState(rs.getString(User.USER_STATE));				Modified by Arun on 08-MAY-2009
			user.setState(rs.getString(User_23Feb09.USER_STATE));		
			//user.setTel(rs.getString(User.USER_TEL));					Modified by Arun on 08-MAY-2009
			user.setTel(rs.getString(User_23Feb09.USER_TEL));			
			//user.setAddr(rs.getString(User.USER_ADDRESS));			Modified by Arun on 08-MAY-2009
			user.setAddr(rs.getString(User_23Feb09.USER_ADDRESS));		
			//user.setZip(rs.getString(User.USER_ZIP));					Modified by Arun on 08-MAY-2009
			user.setZip(rs.getString(User_23Feb09.USER_ZIP));			
			user.setPwd(password);
			//user.setPwd(password);								Modified by Arun on 08-MAY-2009
			rs.close();
			stmt.close();

		} catch (Exception e) {

			Log.info(e.getMessage());

		}

		return user;
	}

}